//
//  RBGTrieController.m
//  BateauGenerique
//
//  Created by Kevin De Jesus Ferreira on 08/06/2014.
//  Copyright (c) 2014 Kevin De Jesus Ferreira. All rights reserved.
//

#import "RBGTrieController.h"
#import "RBGAppDelegate.h"
#import "UIImageView+WebCache.h"
#import "RBGNavigationBar.h"
@interface RBGTrieController ()<UITableViewDelegate,UITableViewDataSource>{
    RBGAppDelegate *appDelegate;
    
}

@end

@implementation RBGTrieController


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    appDelegate = (RBGAppDelegate *)[[UIApplication sharedApplication] delegate];
    
    [self.fond setImageWithURL:[NSURL URLWithString:appDelegate.donnees.parametres.imageFond]];
    self.tableView.backgroundColor = [UIColor clearColor];
    
    [self setTitle:@"Trier"];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SimpleCell"];
    
    if(indexPath.row == 0){
        [cell.textLabel setText:@"Croissant"];
    }else{
        [cell.textLabel setText:@"Décroissant"];
    }
    
    [cell.textLabel setTextColor:appDelegate.donnees.parametres.fontColorDeux];
    UIView *selectedView = [[UIView alloc]init];
    selectedView.backgroundColor = appDelegate.donnees.parametres.backgroundColorDeux;
    cell.selectedBackgroundView =  selectedView;
    
    [cell setBackgroundColor:[UIColor clearColor]];
    
    return cell;
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 2;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [self.titles count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return [self.titles objectAtIndex:section];
}

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)] ;
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 3, tableView.bounds.size.width - 10, 14)] ;
    [label setFont: [UIFont fontWithName:@"Arial" size:13.0f]];
    label.text = [self.titles objectAtIndex:section];
    label.textColor = appDelegate.donnees.parametres.fontColorDeux;
    label.backgroundColor = [UIColor clearColor];
    [headerView setBackgroundColor:appDelegate.donnees.parametres.backgroundColorDeux];
    [headerView addSubview:label];

    return headerView;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
   // Faire le delegate
    
    if(indexPath.row == 0)
        [self.delegate receiveTrie:[self.titles objectAtIndex:indexPath.section] andSens:YES];
    else
        [self.delegate receiveTrie:[self.titles objectAtIndex:indexPath.section] andSens:NO];
    
    [self.controller popViewControllerAnimated:YES];

}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    if(([self.titles count] - 1) == section)
        return 100 *2;
    return 0.01;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView * view = [[UIView alloc]init];
    [view setBackgroundColor:[UIColor clearColor]];
    return view;
}
@end
